package com.openwater;

import android.graphics.PointF;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MyGestureDetector implements OnTouchListener{

    private static final String TAG = MyGestureDetector.class.toString();
    private GestureAction mCurrAction = GestureAction.ACTION_NONE; 

    public MyGestureDetector(View v){
        v.setOnTouchListener(this);
    }

    private OnActionListener mOnActionListener = null;
    public void setOnActionListener(OnActionListener l){
        mOnActionListener = l;
    }

    public void removeActionListener(){
        mOnActionListener = null;
    }

    public interface OnActionListener{
        void onActionUp(float x,float y);
        void onActionDown(float x,float y);
        void onActionMove(float x,float y);
        void onActionFling(float velocityX,float velocityY);
    }

    public GestureAction getCurrAction(){
        return mCurrAction;
    }

    private PointF prev = new PointF();
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (isListenerWork()) {
            if (mGestureDetector.onTouchEvent(event)) {
                Log.d(TAG, "GestureDetector handle event");
                return true;
            }
            final float x = event.getX();
            final float y = event.getX();
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_UP:
                    mOnActionListener.onActionUp(x,y);
                    break;
                case MotionEvent.ACTION_DOWN:
                    mOnActionListener.onActionDown(x,y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    float dx = x - prev.x;
                    float dy = y - prev.y;
                    mOnActionListener.onActionMove(dx, dy);
                    break;
                default:
                    break;
            }
            prev.set(x, y);
        }
        return true;
    }

    private boolean isListenerWork(){
        return mOnActionListener != null;
    }

    private GestureDetector mGestureDetector = new GestureDetector(new OnGestureListener() {
        
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
        
        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }
        
        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            mOnActionListener.onActionFling(velocityX,velocityY);
            return false;
        }
        
        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
    });





    /**Gesture Action Enumerate status*/
    public enum GestureAction{

        /**action none*/
        ACTION_NONE,

        /**action up*/
        ACTION_UP,

        /**action down*/
        ACTION_DOWN,

        /**action move*/
        ACTION_MOVE,

        /**action zoom*/
        ACTION_ZOOM,

        /**action rotate*/
        ACTION_ROTATE,

        /**two-Finger push screen*/
        ACTION_PUSH,

        /**action fling*/
        ACTION_FLING
    }

}
